package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.query.impl.AttendanceQuery;
import cn.wolfcode.crm.service.IAttendanceService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UserContext;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("attendance")
public class AttendanceController {
    @Autowired
    private IAttendanceService service;

    @RequiresPermissions(value = {"attendance:view","签到页面"},logical= Logical.OR)
    @RequestMapping("view")
    public String view() {
        return "attendance";
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult query(AttendanceQuery qo) {
        return service.query(qo);
    }

    @RequestMapping("sign")
    @ResponseBody
    public JSONResult sign() {
        if (service.verify()!=null){
            return new JSONResult(false, "您已签到,请勿重复签到");
        }
        try {
            service.sign();

        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false, "签到失败");
        }
        return new JSONResult();
    }

    @RequestMapping("signOut")
    @ResponseBody
    public JSONResult signOut() {
        if (service.verify()==null){
            return new JSONResult(false, "您尚未签到,请先签到");
        }
        try {
            service.signOut();

        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false, "签退失败");
        }
        return new JSONResult();
    }
}
